package com.gopay.scheduler.job;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.cps.dao.autosettle.AsBankLimitConfigDAO;
import com.gopay.common.cps.dao.autosettle.AsBankSettleDailyStatsDao;
import com.gopay.common.cps.manager.holiday.HolidayInfomationUtilManager;
import com.gopay.common.domain.autosettle.AsBankLimitConfig;
import com.gopay.common.domain.autosettle.AsBankSettleDailyStats;
import com.gopay.scheduler.GopayBaseJob;
import com.ibm.icu.util.Calendar;

/** 
 * @fileName  AutoSettleUpdateLastDayActualAmt.java
 * @author  张建龙 
 * @date 创建时间：2016年2月18日 下午4:44:40
 */
public class AutoSettleUpdateLastDaySettleAmtJob extends GopayBaseJob {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource(name = "asBankLimitConfigDAO")
    private AsBankLimitConfigDAO asBankLimitConfigDAO;
	@Resource(name = "asBankSettleDailyStatsDao")
	private AsBankSettleDailyStatsDao asBankSettleDailyStatsDao;
	@Resource(name = "holidayInformationUtilManager")
	private HolidayInfomationUtilManager holidayInformationUtilManager;
	
	@Override
	@Transactional
	protected void doJobInternal() {
		
		
		Date date = new Date();
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		
		Date settleDate = calendar.getTime();
		logger.info("自动结算的日期为："+settleDate.toString());
		boolean flag = holidayInformationUtilManager.isAutoSettleHoliday(settleDate);
		if(flag){
			return ;
		}
		logger.info("日期"+settleDate.toString()+"不是节假日");
		List<AsBankLimitConfig> asBankLimitConfigList = asBankLimitConfigDAO.findAll(AsBankLimitConfig.class);
		List <AsBankSettleDailyStats> asBankSettleDailyStatsList =new ArrayList<AsBankSettleDailyStats>();
		asBankSettleDailyStatsList.addAll(asBankSettleDailyStatsDao.getAsBankSettleDailyStats(settleDate, settleDate));
		
		for (AsBankLimitConfig asBankLimitConfig : asBankLimitConfigList) {
			logger.info("AsBankLimitConfig数据："+asBankLimitConfig.toString());
			InsertAsBankSettleDailyStats(asBankLimitConfig,asBankSettleDailyStatsList, date, settleDate);
		}
		
		if(asBankSettleDailyStatsList.size()>0){
			asBankSettleDailyStatsDao.saveOrUpdateAll(asBankSettleDailyStatsList);
			logger.info("更新"+asBankSettleDailyStatsList.size()+"条数据成功");
		}
		
	}

	public void InsertAsBankSettleDailyStats(AsBankLimitConfig asBankLimitConfig,
			List <AsBankSettleDailyStats> asBankSettleDailyStatsList,Date date,Date settleDate){
		for (AsBankSettleDailyStats asBankSettleDailyStats : asBankSettleDailyStatsList) {
			if(asBankLimitConfig.getBankCode().equals(asBankSettleDailyStats.getBankCode())){
				if(asBankLimitConfig.getSettleAmt()!=null){
					asBankSettleDailyStats.setSettleAmt(asBankLimitConfig.getSettleAmt());
					asBankSettleDailyStats.setLastUpdateTime(date);
				}
				logger.info("asBankSettleDailyStats数据更新Ready："+asBankSettleDailyStats.toString());
				return ;
			}
		}
		AsBankSettleDailyStats asBankSettleDailyStats = new AsBankSettleDailyStats();
		
		asBankSettleDailyStats.setBankCode(asBankLimitConfig.getBankCode());
		asBankSettleDailyStats.setLastUpdateTime(date);
		asBankSettleDailyStats.setCreateTime(date);
		asBankSettleDailyStats.setSettleAmt(asBankLimitConfig.getSettleAmt());
		asBankSettleDailyStats.setSettleDate(settleDate);
		
		if(null!=asBankLimitConfig.getSettleAmt()){
			asBankSettleDailyStatsList.add(asBankSettleDailyStats);
			logger.info("asBankSettleDailyStats数据插入Ready："+asBankSettleDailyStats.toString());
		}else{
			logger.info("asBankSettleDailyStats数据不更新："+asBankSettleDailyStats.toString());
		}
	}
	
}
